package cn.edu.scnu.controller;

import cn.edu.scnu.Service.UserService;
import cn.edu.scnu.entity.Return;
import cn.edu.scnu.entity.User;
import cn.edu.scnu.util.JwtUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    //注册功能
    @PostMapping("/register")
    public Return register(String username, String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return userService.register(user);
    }
    //登录功能
    @GetMapping("/login")
    public Return login(@RequestParam String username, String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return userService.login(user);
    }
    //获取用户详细信息
    @GetMapping("/userInfo")
    public Return userInfo(@RequestHeader(name="Authorization") String token){
        String username = (String) JwtUitl.parseToken(token).get("username");
        User user = userService.findByUserName(username);
        return new Return().sussess(user);
    }
    //更新用户资料
    @PostMapping("/updateUser")
    public Return updateUser(@RequestHeader(name="Authorization") String token, @RequestBody User user){
        String oldName = (String) JwtUitl.parseToken(token).get("username");
        return userService.updateUser(oldName, user);
    }
    //test
    @GetMapping("/test")
    public Return test(@RequestParam String birthday){
        System.out.println(birthday);
        return new Return();
    }
}
